<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" :where="defaultWhere" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        row-click-checked
        highlight-current-row
        border
        stripe
        :where="defaultWhere"
        :loadOnCreated="false"
      >
        <template #realname="{ row }">
          <span class="pointer blue" @click="showStudentDetail(row.member_id)">
            {{ row.member_realname }}
          </span>
        </template>
        <template #parent="{ row }">
          <span class="pointer blue" @click="showStudentDetail(row.parent_id)">
            {{ row.parent_realname }}
          </span>
        </template>

        <template #toolbar="{}">
          <el-button type="info" @click="exportExcel" :icon="DocumentCopy">
            导出表格
          </el-button>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 回访 -->
    <judge v-model="showJudge" :job="current" type="judge" @done="reload" />
    <!-- 学生详情 -->
    <detail v-model="showDetail" :student-id="currentId" @done="reload" />
    <!-- 资产详情 -->
    <assetc v-model="showAssetc" :user-id="currentId" :type="0" />
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import Search from './search.vue';
  import { DocumentCopy } from '@element-plus/icons-vue';
  import Judge from '@/views/company/apply/components/pay.vue';
  import Detail from '@/views/student/components/detail.vue';
  import Assetc from '@/views/student/components/assetc-detail.vue';
  import { pageBounsList, exportBounsList } from '@/api/money';
  import { EleMessage } from 'ele-admin-plus/es';

  /** 表格实例 */
  const tableRef = ref(null);

  const props = defineProps({
    defaultWhere: Object
  });

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [
      {
        prop: 'site',
        label: '城市',
        width: 95,
        align: 'center'
      },
      {
        prop: 'job_code',
        label: '工作编号',
        align: 'center',
        width: 125
      },
      {
        prop: 'job_company',
        label: '相关企业'
      },
      {
        label: '职位名称',
        prop: 'job_title',
        width: 180
      },
      {
        width: 100,
        slot: 'realname',
        label: '工作人'
      },
      {
        slot: 'parent',
        label: '推荐人',
        width: 100
      },

      {
        prop: 'cate',
        label: '类型',
        align: 'center',
        width: 120
      },
      {
        prop: 'amount',
        label: '奖金',
        width: 90
      },
      {
        prop: 'create_time',
        label: '申请时间',
        width: 144
      },
      {
        prop: 'paid_time',
        label: '处理时间',
        width: 144
      }
    ];

    if (status.value !== 0) {
      cols.push({
        slot: 'judge_user',
        label: '审核人',
        width: 95
      });
      cols.push({
        slot: 'judge_time',
        label: '审核时间',
        width: 110
      });
    }

    return cols;
  });

  const status = ref(0);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 当前编辑数据 */
  const currentId = ref(null);

  /** 是否显示编辑弹窗 */
  const showJudge = ref(false);

  /** 是否显示编辑弹窗 */
  const showDetail = ref(false);
  /** 是否显示编辑弹窗 */
  const showAssetc = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageBounsList({
      ...where,
      ...orders,
      page,
      limit
    });
  };
  /** 导出表格 */
  const exportExcel = () => {
    const loading = EleMessage.loading('请求中..');
    tableRef.value?.fetch(({ where }) => {
      // 方法的参数与数据源的完全一致
      // 请求查询全部接口
      console.log(where);
      var requestData = where;
      requestData.limit = 99999999;
      requestData.page = 1;

      exportBounsList(requestData)
        .then((res) => {
          loading.close();
          window.open(res);
        })
        .catch(() => {
          loading.close();
        });
    });
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    status.value = where ? where.status : 100;
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const showStudentDetail = (id) => {
    currentId.value = id ?? 0;
    showDetail.value = true;
  };
</script>

<script>
  export default {
    name: 'SystemUser'
  };
</script>
